<template>
  <div class="login-container">
    <!-- 1. 头部组件 -->
    <van-nav-bar title="登录">
      <template #left>
        <img class="logo" src="@/assets/images/logo.png" />
      </template>
    </van-nav-bar>
    <!-- 2. 输入框组件 -->
    <van-form @submit="onSubmitFn">
      <van-field
        v-model="formLogin.tel"
        type="tel"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="rules.tel"
      />
      <van-field
        v-model="formLogin.pwd"
        type="password"
        label="密码"
        placeholder="请输入密码"
        required
        :rules="rules.password"
      />
      <div style="margin: 16px">
        <van-button
          :loading="isloading"
          round
          block
          type="info"
          native-type="submit"
          loading-text="加载中..."
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
// 导入提示函数
import { Notify } from 'vant'
// 导入登录接口
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      isloading: false,
      formLogin: {
        tel: '13888888888',
        pwd: '246810' // 默认只能用246810后台规定了
      },
      rules: {
        tel: [
          {
            required: true,
            message: '请填写手机号',
            trigger: 'onChange',
            pattern: /^1[3-9]\d{9}$/
          }
        ],
        password: [
          {
            required: true,
            message: '请填写验证码',
            trigger: 'onChange',
            pattern: /^\d{6}$/
          }
        ]
      }
    }
  },
  methods: {
    ...mapActions(['setAlltoken']),
    async onSubmitFn () {
      this.isloading = true
      try {
        await this.setAlltoken(this.formLogin)
        // const resp = await loginAPI({
        //   tel: this.formLogin.tel,
        //   pwd: this.formLogin.pwd
        // })
        // 登录成功的提示信息
        Notify({ type: 'success', message: '登录成功!' })
        // setTimeout(() => {
        //   location.href = 'http://localhost:8080/#/layout/home'
        // }, 1000)
        // 点击按钮成功后跳转至首页
        this.$router.push({
          path: this.$route.query.from || '/layout'
        })
      } catch (err) {
        console.log(err)
        // 登录失败的提示信息
        Notify({ type: 'danger', message: '手机号或验证码错误!' })
      } finally {
        // 不管成功与否都关闭按钮的加载中...
        this.isloading = false
      }
      // console.log(window)
      // console.log(document)
    }
  }
}
</script>

<style scoped lang="less">
.logo {
  width: 100px;
  height: 30px;
}
// .van-nav-bar {
//   background: #007bff;
// }

//  /deep/ 就是把data-v-hash值选择器写到类名的前面
//  .van-nav-bar__title {
//   color: white;
// }
</style>
